$(function () {
    var emp_datagrid = $("#emp_datagrid");
    var emp_dialog = $("#emp_dialog");
    var emp_form = $("#emp_form");
// 数据 表
    emp_datagrid.datagrid({
        url: "/employee/query.do",
        fitColumns: true, // 自适应列
        fit: true,
        striped: true,  // 斑马线
        pagination: true,
        rownumbers: true,
        pageSize: 10,
        pageNumber: 1,
        singleSelect: true, // 单选
        toolbar: "#emp_toolbar",
        columns: [
            [
                // {field: 'id', checkbox: true},
                {field: 'username', title: '用户名', width: 80},
                {field: 'realname', title: '真实姓名', width: 80},
                {field: 'email', title: '邮箱', width: 80},
                {
                    field: 'dept', title: '部门名称', width: 80, sortable: true,
                    formatter: function (value, row, index) {
                        return value ? value.name : "";
                    }
                },
                {field: 'tel', title: '电话', width: 80, sortable: true},
                {field: 'hiredate', title: '入职时间', width: 80, sortable: true},
                {
                    field: 'state', title: '状态', width: 80, sortable: true,
                    formatter: function (value, row, index) {
                        return value ? "在职" : "<span style='color: #F00;'>离职</span>";
                    }
                },
                {
                    field: 'admin', title: '是否是管理员', width: 80, sortable: true,
                    formatter: function (value, row, index) {
                        return value ? "<span style='color: #F00;'>是</span>" : "否";
                    }
                },
            ]
        ],
        onSelect: function (index, row) {
            $("#state_btn").linkbutton({text: row.state ? "离职" : '复职'});
            $("#editBtn").linkbutton(row.state ? "enable" : 'disable');
        }
    });
    // 对话 框

    emp_dialog.dialog({
        width: 320,
        height: 420,
        top: 100,
        buttons: "#form_btns",
        closed: true,
        onClose: function () {
            emp_form.form("clear");
        }
    });

    function openSetTitle(str) {
        emp_dialog.dialog("setTitle", str)
        emp_dialog.dialog("open")
    }

    var objMethod = {
        add: function () {
            $(".myPassword").show();
            openSetTitle("新增员工")
        },
        edit: function () {
            $(".myPassword").hide();
            var row = emp_datagrid.datagrid("getSelected");
            if (!row.state) {
              return;
            }
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return
            }
            if (row.dept) {
                row["dept.id"] = row.dept.id
            }
            // 回选数据
            emp_form.form("load", row);
            // 回选 角色
            $.get('/role/selectByEmployeeId.do', {id: row.id}, function (data) {
                $('#role_combobox').combobox('setValues', data);
                openSetTitle("编辑员工")
            })
        },
        changeState: function () {
            var row = emp_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning');
                return
            }
            $.messager.confirm('确认', '你确认要操作吗？', function (r) {
                if (!r) return;
                $.get('/employee/changeState.do', {id: row.id}, function (data) {
                    if (data.success) {
                        $.messager.alert("温馨提示", '操作成功', 'info', function () {
                            emp_datagrid.datagrid("reload");
                        })
                    } else {
                        $.messager.alert("温馨提示", '操作失败', 'info', function () {
                        })
                    }
                })
            })
        },
        save: function () {
            // 提交表单
            emp_form.form("submit", {
                url: "/employee/saveOrUpdate.do",
                onSubmit: function (params) {
                    var values = $('#role_combobox').combobox('getValues');
                  console.log(values);
                  values.forEach(function (id, index) {params[`roles[${index}].id`] = id})
                    return emp_form.form('validate');
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", '保存成功', 'info', function () {
                            objMethod.cancel();
                            emp_datagrid.datagrid("reload");  // 刷新
                        })
                    } else {
                        $.messager.alert("温馨提示", '保存失败', 'info', function () {
                        })
                    }
                }
            })
        },
        cancel: function () {
            emp_dialog.dialog("close")
        },
        query: function () {
            var keyword = $("#keyword").textbox('getValue');
            var minHireDate = $("#minHireDate").textbox('getValue');
            var maxHireDate = $("#maxHireDate").textbox('getValue');
            var deptId = $("#deptId").textbox('getValue');
            $('#emp_datagrid').datagrid('load', {
                keyword: keyword,
                minHireDate: minHireDate,
                maxHireDate: maxHireDate,
                deptId: deptId
            })
        },
        upload: function () {
            var data = new FormData();
            data.append('file', $("#uploadFile")[0].files[0])
            console.log($("#uploadFile"));
            $.ajax({
                url: "/employee/uploadXls.do",
                method: 'POST',
                contentType: false,
                data: data,
                processData: false,
                success: function (data) {
                    console.log(data);
                }
            })
        }
    };
    // 绑定时间
    $('a[data-cmd]').click(function () {
        objMethod[$(this).data('cmd')]();
    })
})

